package org.ccay.mail.service.impl.internal;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.ccay.core.exception.ApplicationException;
import org.ccay.core.log.ILogger;
import org.ccay.core.log.CcayLoggerFactory;
import org.ccay.core.util.CollectionUtil;
import org.ccay.core.util.StringUtil;
import org.ccay.mail.MailApplicationException;
import org.ccay.mail.MailVO;
import org.ccay.mail.MeetingMailVO;

/**
 * 邮件辅助工具
 * @author chaowangbang
 * @since 2011-11-24
 */
public final class MailUtil {
	private static final ILogger logger = CcayLoggerFactory.getLogger("MailUtil");
	private MailUtil(){
	}

	/** 
	 * 检查邮件参数
	 * @author chaowangbang
	 * @since 2011-11-24
	 * @param newMail
	 */
	public static void checkMail(MailVO newMail) throws ApplicationException {
		
		//验证邮件格式 如果有错误的移除
		newMail.setTo(validateAddesss(newMail.getTo()));
		newMail.setCc(validateAddesss(newMail.getCc()));
		newMail.setBcc(validateAddesss(newMail.getBcc()));
		
		//清除过错误邮件地址后  再次验证是否有收件人
		if(!hasReceivers(newMail) ){
			//无收件人
			throw new MailApplicationException("ccay.mail.00010004");
		}
		if( noSubject(newMail) ){
			//无主题
			throw new MailApplicationException("ccay.mail.00010003");
		} 
	}


	/** 
	 * 检查邮件参数
	 * @author chaowangbang
	 * @since 2012-10-30
	 * @param meetingMainVO
	 */
	public static void checkMeetingMail(MeetingMailVO meetingMailVO) throws ApplicationException {
		//当普通会议验证
		checkMail(meetingMailVO);
		if( noSubject(meetingMailVO) || noMeetingAddress(meetingMailVO) || noMeetingDate(meetingMailVO)){
			//无主题、无会议地址、无会议开始时间、无会议结束时间
			throw new MailApplicationException("ccay.mail.00010001");
		} 
	}
	
	private static boolean noMeetingDate(MeetingMailVO meetingMailVO){
		return noMeetingStartDate(meetingMailVO) || noMeetingEndDate(meetingMailVO);
	}

	/**
	 * 无会议开始时间
	 * @param meetingMainVO
	 * @return
	 */
	private static boolean noMeetingStartDate(MeetingMailVO meetingMailVO) {
		return StringUtil.isNullOrEmpty(meetingMailVO.getMeetingStartDate().toString());
	}
	
	/**
	 * 无会议结束时间
	 * @param meetingMainVO
	 * @return
	 */
	private static boolean noMeetingEndDate(MeetingMailVO meetingMailVO) {
		return StringUtil.isNullOrEmpty(meetingMailVO.getMeetingEndDate().toString());
	}
	
	/**
	 * 无会议地址
	 * @param meetingMainVO
	 * @return
	 */
	private static boolean noMeetingAddress(MeetingMailVO meetingMainVO) {
		return StringUtil.isNullOrEmpty(meetingMainVO.getMeetingAddress());
	}

	
	/**
	 * 无主题
	 * @param newMail
	 * @return
	 */
	private static boolean noSubject(MailVO newMail) {
		return StringUtil.isNullOrEmpty(newMail.getSubject())
				&& StringUtil.isNullOrEmpty(newMail.getTemplateName());
	}
 

	/** 
	 * 判断有无收件人
	 * @param newMail
	 * @return
	 */
	private static boolean hasReceivers(MailVO newMail) {
		return !CollectionUtil.isNullOrEmpty(newMail.getTo()) ||
				!CollectionUtil.isNullOrEmpty(newMail.getCc()) ||
				!CollectionUtil.isNullOrEmpty(newMail.getBcc());
	}
	 
	private static final Pattern regex = Pattern.compile("\\b(^['_A-Za-z0-9-]+(\\.['_A-Za-z0-9-]+)*@([A-Za-z0-9-])+(\\.[A-Za-z0-9-]+)*((\\.[A-Za-z0-9]{2,})|(\\.[A-Za-z0-9]{2,}\\.[A-Za-z0-9]{2,}))$)\\b");
	/**
	 * 检查email地址格式 无效的移除
	 * @param mails
	 * @throws MailApplicationException
	 */
	public static String[] validateAddesss(String[] mails){
		if(CollectionUtil.isNullOrEmpty(mails)){
			return new String[0];
		}
		List<String> newEmails =  new ArrayList<String>(); 
		 for(String mail : mails){
			 if(!regex.matcher(mail).matches()){
				 logger.warn("remove invalidate email addesss:"+mail);
			 }else{
				 newEmails.add(mail);			 
			 }
		 }
		 return newEmails.toArray(new String[newEmails.size()]);
	}
	/**
	 * 验证文件路径是否存在 
	 * @param filePath
	 * @return
	 */
	public static boolean validateAttachment(String filePath){
		if(StringUtil.isNullOrEmpty(filePath) || !new File(filePath).isFile()){
			logger.error("Attachments path:" + filePath + " not exists!");
			return false;
		}
		return true;
	}
}
